import java.net.URI;
import java.util.Date;

import javax.ws.rs.core.UriBuilder;

import co.edu.unal.franquicias.visa.domain.CreditCard;
import co.edu.unal.franquicias.visa.domain.CreditCardTransaction;
import co.edu.unal.franquicias.visa.domain.DebitCard;
import co.edu.unal.franquicias.visa.domain.DebitCardTransaction;
import co.edu.unal.franquicias.visa.representations.CreditCardTransactionRepresentation;
import co.edu.unal.franquicias.visa.representations.Representation;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class VisaClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Initializing ...");
//		testGet();
		 testPostCreditCard();
		 testPostDebitCard();

		System.out.println("Finishing ...");
	}

	private static CreditCardTransaction createNewCreditCardTransaction() {
		CreditCard card = new CreditCard();
		card.setDate(new Date());
		card.setNameOnCard("Jhon");
		card.setNumber("3333333");
		card.setVerificationCode("569");

		CreditCardTransaction creditCardTransaction = new CreditCardTransaction(
				card, new Float(125), new Date());

		return creditCardTransaction;
	}

	private static DebitCardTransaction createNewDebitCardTransaction() {
		DebitCard card = new DebitCard();
		card.setNumber("34534535");

		DebitCardTransaction debitCardTransaction = new DebitCardTransaction(
				card, new Float(125), new Date());

		return debitCardTransaction;
	}

	private static URI getBaseURI() {
		return UriBuilder.fromUri("http://localhost:8080/VisaWS/").build();
	}

	private static void testPostCreditCard() {
		try {
			ClientConfig config = new DefaultClientConfig();
			Client client = SettingsVisa.getClient();
			WebResource service = client.resource(getBaseURI());

			service = service.path("creditCard/");

			ClientResponse response = service.type(
					Representation.VISA_MEDIA_TYPE).post(ClientResponse.class,
					createNewCreditCardTransaction());

			CreditCardTransactionRepresentation entity = response
					.getEntity(CreditCardTransactionRepresentation.class);

			System.out.println(entity.getTransaction().getValidationStatus());

			System.out.println(response.getStatus());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void testPostDebitCard() {
		try{
		Client client = SettingsVisa.getClient();
		WebResource service = client.resource(getBaseURI());

		service = service.path("debitCard/");

		ClientResponse response = service.type(Representation.VISA_MEDIA_TYPE)
				.post(ClientResponse.class, createNewDebitCardTransaction());
		
		if(response.getStatus() != 200){
			throw new Exception();
		}

		CreditCardTransactionRepresentation entity = response
				.getEntity(CreditCardTransactionRepresentation.class);

		System.out.println(entity.getTransaction().getValidationStatus());

		System.out.println(response.getStatus());
		}catch (Exception e) {
			System.out.println("Error");
		}
	}

	private static void testGet() {
		try {

			Client client = SettingsVisa.getClient();
//			client= Client.create();

			WebResource webResource = client
					.resource("http://localhost:8080/VisaWS/creditCard/");

			ClientResponse response = webResource.accept(
					Representation.VISA_MEDIA_TYPE).get(ClientResponse.class);

			if (response.getStatus() != 200) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ response.getStatus());
			}

			String output = response.getEntity(String.class);

			System.out.println("Output from Server .... \n");
			System.out.println(output);

		} catch (Exception e) {

			e.printStackTrace();

		}
	}

}
